from flask import jsonify, make_response, request, g
from flask_restful import Resource
from apps.settingApi.switchCase.casePrimaryKey import caseGetPrimaryKeyData
from apps.settingApi.switchCase.serializeParameter import getArgsData

from apps.settingApi.switchCase.settingCurd import getList, postList, getPk, deletePk, putPk
from apps.settingApi.api.dict.model import dictTypeModel, dictDataModel
from comm.decorator import login_required

dictViewModel = dictTypeModel
dictDataViewModel = dictDataModel
# 字典操作=========================================================
class dictListView(Resource):
    # 查询用户列表
    @login_required()
    def get(self):
        # 获取所有的参数
        args = request.args
        objData ,err = getList(dictViewModel,args)
        if err!=None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功','result': objData}), 200)
    @login_required()
    def post(self):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)

        objData,err = postList(dictViewModel,args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

class dictPkView(Resource):
    @login_required()
    def get(self, pk):
        objData, err = getPk(dictTypeModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def put(self, pk):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        objData, err = putPk(dictTypeModel, pk, args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def delete(self, pk):
        objData, err = deletePk(dictTypeModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

# 字典数据==========================================================
class dictDataListView(Resource):
    # 查询用户列表
    # 查询用户列表
    @login_required()
    def get(self):
        # 获取所有的参数
        args = request.args
        objData, err = getList(dictDataViewModel, args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def post(self):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args['create_user'] = g.userid
        args['id'] = caseGetPrimaryKeyData()
        print(args)
        objData, err = postList(dictDataViewModel, args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

class dictDataPkView(Resource):

    @login_required()
    def get(self, pk):
        objData, err = getPk(dictDataViewModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def put(self, pk):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        objData, err = putPk(dictDataViewModel, pk, args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def delete(self, pk):
        objData, err = deletePk(dictDataViewModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

